package br.com.viewcar.entity;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "GRUPO", schema = "CHECKROTA")
@NamedQuery(name = "Grupo.findByCliente", query = "SELECT p FROM Grupo p WHERE p.cliente.id = ?")
public class Grupo implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="ID", nullable = false)
	private Integer id;

	@Column(name="NOME", nullable = false, length = 50)
	private String nome;

	@ManyToOne
	@JoinColumn(name="ID_CLIENTE")
	private Cliente cliente;

	@ManyToMany
	@JoinTable(name = "GRUPO_PERMISSAO",schema = "VIEWCAR", 
			joinColumns=@JoinColumn(name="ID_GRUPO"), inverseJoinColumns=@JoinColumn(name="ID_PERMISSAO"))
	private List<Permissao> permissoes;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}


	public List<Permissao> getPermissoes() {
		return permissoes;
	}

	public void setPermissoes(List<Permissao> permissoes) {
		this.permissoes = permissoes;
	}

	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	public int hashCode() {

		int result = 1;
		result = 31 * result + ((id == null) ? 0 : id.hashCode());
		result = 31 * result + ((nome == null) ? 0 : nome.hashCode());
		result = 31 * result + ((cliente == null) ? 0 : cliente.hashCode());

		return result;
	}

	public boolean equals(Object obj) {

		if (this == obj)
			return true;

		if (obj == null)
			return false;

		if (getClass() != obj.getClass())
			return false;

		Grupo other = (Grupo) obj;

		return ((id == null && other.id == null) || (id != null && id.equals(other.id))) &&
		((nome == null && other.nome == null) || (nome != null && nome.equals(other.nome))) &&
		((cliente == null && other.cliente== null) || (cliente != null && cliente.equals(other.cliente)));
	}
}